package brau.view;

import java.io.Serializable;

import javax.inject.Inject;

import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.MapModel;

import br.gov.frameworkdemoiselle.annotation.PreviousView;
import br.gov.frameworkdemoiselle.stereotype.ViewController;
import br.gov.frameworkdemoiselle.template.AbstractEditPageBean;
import brau.business.EnderecoBC;
import brau.domain.Endereco;

@ViewController
@PreviousView("/endereco_list.xhtml")
@SuppressWarnings("serial")
public class EnderecoEditMB extends AbstractEditPageBean<Endereco, Long> implements Serializable {

	@Inject
	private EnderecoBC bc;

	private Endereco enderecoGMap = new Endereco();
	private Integer count = 0;
	private MapModel simpleModel = new DefaultMapModel();
	
	
	public String delete() {
		this.bc.delete(getId());
		return getPreviousView();
	}
	
	public String insert() {
		this.bc.insert(getBean());
		return getPreviousView();
	}
	
	public String update() {
		System.out.println(getBean().getEnderecoCompleto());
		this.bc.update(getBean());
		return getPreviousView();
	}
	
	@Override
	protected void handleLoad() {
		setBean(this.bc.load(getId()));
	}
	

	public void buscarEnderecoByLatLng() {
		Endereco endereco = new Endereco();
        endereco = bc.buscarEnderecoByPontoXY(Double.valueOf(getBean().getPontoX()), Double.valueOf(getBean().getPontoY()));
        if ( endereco == null || endereco.getId() == 0l) endereco = bc.buscarEnderecoGMapByPontoXY(Double.valueOf(getBean().getPontoX()), Double.valueOf(getBean().getPontoY()));
        setBean(endereco);
	}
	
	public void buscarEnderecoGMapByEndereco() {

		if ( ( getBean().getLogradouro() != null && ! getBean().getLogradouro().isEmpty() ) ||
			 ( getBean().getCep() != null && ! getBean().getCep().isEmpty() ) ) {

			enderecoGMap = bc.buscarEnderecoGMapByEndereco(getBean());
			
			if (enderecoGMap != null) {
				if ( enderecoGMap.getLogradouro() != null && ! enderecoGMap.getLogradouro().isEmpty()) getBean().setLogradouro(enderecoGMap.getLogradouro());
				if ( enderecoGMap.getBairro() != null && ! enderecoGMap.getBairro().isEmpty()) getBean().setBairro(enderecoGMap.getBairro());
				if ( enderecoGMap.getLocalidade() != null && ! enderecoGMap.getLocalidade().isEmpty()) getBean().setLocalidade(enderecoGMap.getLocalidade());
				if ( enderecoGMap.getUf() != null && ! enderecoGMap.getUf().isEmpty()) getBean().setUf(enderecoGMap.getUf());
				if ( enderecoGMap.getPais() != null && ! enderecoGMap.getPais().isEmpty()) getBean().setPais(enderecoGMap.getPais());
				if ( enderecoGMap.getCep() != null && ! enderecoGMap.getCep().isEmpty()) getBean().setCep(enderecoGMap.getCep());
				if ( enderecoGMap.getNumero() != null && ! enderecoGMap.getNumero().isEmpty()) getBean().setNumero(enderecoGMap.getNumero());
				if ( enderecoGMap.getPontoX() != null && enderecoGMap.getPontoX() != 0) getBean().setPontoX(enderecoGMap.getPontoX());
				if ( enderecoGMap.getPontoY() != null && enderecoGMap.getPontoY() != 0) getBean().setPontoY(enderecoGMap.getPontoY());
			}
		}
	}

	
	public void addCount(){
		count++;
	}

//        //Shared coordinates  
//        LatLng coord1 = new LatLng(36.879466, 30.667648);  
//        LatLng coord2 = new LatLng(36.883707, 30.689216);  
//        LatLng coord3 = new LatLng(36.879703, 30.706707);  
//        LatLng coord4 = new LatLng(36.885233, 30.702323);  
//          
//        //Basic marker  
//        simpleModel.addOverlay(new Marker(coord1, "Konyaalti"));  
//        simpleModel.addOverlay(new Marker(coord2, "Ataturk Parki"));  
//        simpleModel.addOverlay(new Marker(coord3, "Karaalioglu Parki"));  
//        simpleModel.addOverlay(new Marker(coord4, "Kaleici"));  
//    }  
 
	public MapModel getSimpleModel() {  
		return simpleModel;
	}

	
	// getters and setters
	
	public Endereco getEnderecoGMap() {
		return enderecoGMap;
	}

	public void setEnderecoGMap(Endereco enderecoGMap) {
		this.enderecoGMap = enderecoGMap;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	
}
